﻿<UserControl 
    x:Class="AbsyntaxExcelAddIn.Core.ExecutionDialogueContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:AbsyntaxExcelAddIn.Core"
    xmlns:cv="clr-namespace:AbsyntaxExcelAddIn.Core.Converters"
    xmlns:res="clr-namespace:AbsyntaxExcelAddIn.Resources;assembly=AbsyntaxExcelAddIn.Resources"
    xmlns:ab="clr-namespace:AbsyntaxExcelAddIn.Core.AttachedBehaviours"
    SnapsToDevicePixels="True"
    UseLayoutRounding="True">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/ExpressionDark.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <Storyboard x:Key="SelectedOn">
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="LogTextBox" Storyboard.TargetProperty="(FrameworkElement.Height)">
                    <SplineDoubleKeyFrame KeyTime="00:00:00.0500000" Value="100"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
            <Storyboard x:Key="SelectedOff">
                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="LogTextBox" Storyboard.TargetProperty="(FrameworkElement.Height)">
                    <SplineDoubleKeyFrame KeyTime="00:00:00.0500000" Value="30"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>

            <cv:BoolToVisibilityConverter x:Key="VisibleCollapsedConverter" TrueValue="Visible" FalseValue="Collapsed" />
            <cv:BoolToVisibilityConverter x:Key="CollapsedVisibleConverter" TrueValue="Collapsed" FalseValue="Visible" />
            <cv:BoolToVisibilityConverter x:Key="HiddenVisibleConverter" TrueValue="Hidden" FalseValue="Visible" />
            <cv:BoolToVisibilityConverter x:Key="VisibleHiddenConverter" TrueValue="Visible" FalseValue="Hidden" />
            <cv:ProjectExecutionStateToPngUriConverter x:Key="ProjectExecutionStateToPngUriConverter" />

            <Style x:Key="ListItemContainerStyle" TargetType="{x:Type ListBoxItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="VerticalContentAlignment" Value="Top" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="ab:ListBoxItemBehaviour.IsBroughtIntoViewWhenSelected" Value="True" />
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border
                                Tag="{Binding}"
                                GotFocus="Border_GotFocus"
                                LostFocus="Border_LostFocus"
                                BorderThickness="0,0,0,1"
                                BorderBrush="DarkGray"
                                Padding="10">

                                <Border.Style>
                                    <Style TargetType="{x:Type Border}">
                                        <Setter Property="Background" Value="{StaticResource NormalBrush}" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                                <Setter Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Border.Style>

                                <GroupBox>
                                    <GroupBox.Header>
                                        <TextBlock Text="{Binding ProjectPath, Mode=OneTime}" TextTrimming="CharacterEllipsis" />
                                    </GroupBox.Header>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="50" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        
                                        <Grid>
                                            <local:InProgressIndicator 
                                                Foreground="PaleGoldenrod"
                                                VerticalAlignment="Top" 
                                                Margin="10" 
                                                Height="26" 
                                                Width="26"
                                                Visibility="{Binding IsExecuting, Converter={StaticResource VisibleHiddenConverter}}" />

                                            <Image 
                                                VerticalAlignment="Top"
                                                Margin="10"
                                                Height="32"
                                                Source="{Binding State, Converter={StaticResource ProjectExecutionStateToPngUriConverter}}"
                                                Visibility="{Binding IsExecuting, Converter={StaticResource HiddenVisibleConverter}}" />
                                        </Grid>

                                        <TextBox 
                                            x:Name="LogTextBox"
                                            Grid.Column="1" 
                                            Margin="5" 
                                            IsReadOnly="True"
                                            TextWrapping="Wrap"
                                            VerticalScrollBarVisibility="Auto"
                                            FontFamily="Courier New"
                                            Foreground="#FF00FF00"
                                            Height="30"
                                            Visibility="{Binding CanExecute, Mode=OneTime, Converter={StaticResource VisibleCollapsedConverter}}"
                                            ab:TextBoxTextWriterBehaviour.TextWriter="{Binding Log}"
                                            ab:TextBoxTextWriterBehaviour.AutoCreate="{Binding AutoCreateLog}"
                                            ab:TextBoxScrollBehaviour.ScrollToEnd="True" />
                                    </Grid>
                                </GroupBox>
                            </Border>
                            
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Trigger.EnterActions>
                                        <BeginStoryboard x:Name="SelectedOn" Storyboard="{StaticResource SelectedOn}"/>
                                    </Trigger.EnterActions>
                                    <Trigger.ExitActions>
                                        <BeginStoryboard x:Name="SelectedOff" Storyboard="{StaticResource SelectedOff}"/>
                                    </Trigger.ExitActions>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>

    <Border BorderThickness="0" Background="{StaticResource ControlBackgroundBrush}">
        <Grid Margin="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <ListBox 
                ItemsSource="{Binding Items}" 
                ItemContainerStyle="{StaticResource ListItemContainerStyle}"
                KeyboardNavigation.TabNavigation="None" 
                SelectionMode="Single"
                VirtualizingStackPanel.IsVirtualizing="False"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                FocusManager.IsFocusScope="True" />
            
            <Grid Grid.Row="1">
                <Grid.Resources>
                    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
                        <Setter Property="Margin" Value="0,10,0,0" />
                        <Setter Property="Width" Value="70" />
                        <Setter Property="Height" Value="20" />
                        <Setter Property="HorizontalAlignment" Value="Right" />
                    </Style>
                </Grid.Resources>
                <Button 
                    Click="CloseButton_Click" 
                    Content="{x:Static res:TextResources.Caption_Close}"
                    Visibility="{Binding IsExecuting, Mode=OneWay, Converter={StaticResource CollapsedVisibleConverter}}"
                    IsCancel="True" />
                <Button 
                    Click="AbortButton_Click" 
                    Content="{x:Static res:TextResources.Caption_Abort}"
                    Visibility="{Binding IsExecuting, Mode=OneWay, Converter={StaticResource VisibleCollapsedConverter}}" />
            </Grid>
        </Grid>
    </Border>
</UserControl>
